package xyz.riceball.generator.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.generator.entity.po.FieldMappingPO;
import xyz.riceball.generator.service.FieldMappingService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 数据库类型字段映射表(FieldMapping)表控制层
 *
 * @author xiaovloud
 * @since 2022-05-13 23:09:10
 */
@RbSysRequest
@RestController
@RequestMapping("/rb/api/generator/fieldMapping")
@Api(tags = "数据库字段映射")
public class FieldMappingController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private FieldMappingService fieldMappingService;

    /**
     * 分页查询所有数据
     *
     * @param pageParam      分页对象
     * @param fieldMappingPO 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation("分页查询所有映射")
    public ApiR selectPage(PageParam pageParam, FieldMappingPO fieldMappingPO) {
        return success(this.fieldMappingService.selectPage(PageParam.of(pageParam), fieldMappingPO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条映射")
    public ApiR selectOne(@PathVariable Serializable id) {
        return success(this.fieldMappingService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param fieldMappingPO 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增映射")
    public ApiR insert(@RequestBody FieldMappingPO fieldMappingPO) {
        return success(this.fieldMappingService.save(fieldMappingPO));
    }

    /**
     * 修改数据
     *
     * @param fieldMappingPO 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改映射")
    public ApiR update(@RequestBody FieldMappingPO fieldMappingPO) {
        return success(this.fieldMappingService.updateById(fieldMappingPO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("删除映射")
    public ApiR delete(@RequestParam("ids") List<Long> ids) {
        return success(this.fieldMappingService.removeByIds(ids));
    }
}

